package org.jetnuke.modules.admin.survey;

import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jforum.JForumExecutionContext;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.dao.PollDAO;
import org.jetnuke.entities.Poll;
import org.jetnuke.util.JetUtil;

public class UpdatePoll implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(UpdatePoll.class);

	int pollID;

	String title;

	String opt_1, opt_2, opt_3, opt_4, opt_5, opt_6, opt_7, opt_8, opt_9,
			opt_10, opt_11, opt_12, opt_13, opt_14, opt_15;

	public void setOpt_1(String opt_1) {
		this.opt_1 = opt_1;
	}

	public void setOpt_10(String opt_10) {
		this.opt_10 = opt_10;
	}

	public void setOpt_11(String opt_11) {
		this.opt_11 = opt_11;
	}

	public void setOpt_12(String opt_12) {
		this.opt_12 = opt_12;
	}

	public void setOpt_13(String opt_13) {
		this.opt_13 = opt_13;
	}

	public void setOpt_14(String opt_14) {
		this.opt_14 = opt_14;
	}

	public void setOpt_15(String opt_15) {
		this.opt_15 = opt_15;
	}

	public void setOpt_2(String opt_2) {
		this.opt_2 = opt_2;
	}

	public void setOpt_3(String opt_3) {
		this.opt_3 = opt_3;
	}

	public void setOpt_4(String opt_4) {
		this.opt_4 = opt_4;
	}

	public void setOpt_5(String opt_5) {
		this.opt_5 = opt_5;
	}

	public void setOpt_6(String opt_6) {
		this.opt_6 = opt_6;
	}

	public void setOpt_7(String opt_7) {
		this.opt_7 = opt_7;
	}

	public void setOpt_8(String opt_8) {
		this.opt_8 = opt_8;
	}

	public void setOpt_9(String opt_9) {
		this.opt_9 = opt_9;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setPollID(int pollID) {
		this.pollID = pollID;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IAction#exec(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IModuleInfo,
	 *      java.lang.String, org.jetnuke.common.IUserInfo,
	 *      org.jetnuke.common.IAdminInfo)
	 */
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		PollDAO pm = DataAccessDriver.getInst().newJPollDAO();
		Poll poll = DataAccessDriver.getInst().newJPollDAO().retrieveByPK(
				pollID);
		poll.setPOLLTITLE(title);
		pm.update(poll);

		Connection cn = null;
		try {
			cn = JForumExecutionContext.getConnection();
			// cn.setAutoCommit(false);
			PreparedStatement ps0 = cn
					.prepareStatement("delete from poll_data where pollID=?");
			ps0.setInt(1, pollID);
			ps0.execute();
			PreparedStatement ps = cn
					.prepareStatement("insert into poll_data values(?,?,0,?)");
			boolean cont = false;
			if (opt_1.length() > 0) {
				ins(ps, pollID, 1, opt_1);
				cont = true;
			}
			if (cont == true && opt_2.length() > 0) {
				ins(ps, pollID, 2, opt_2);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_3.length() > 0) {
				ins(ps, pollID, 3, opt_3);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_4.length() > 0) {
				ins(ps, pollID, 4, opt_4);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_5.length() > 0) {
				ins(ps, pollID, 5, opt_5);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_6.length() > 0) {
				ins(ps, pollID, 6, opt_6);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_7.length() > 0) {
				ins(ps, pollID, 7, opt_7);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_8.length() > 0) {
				ins(ps, pollID, 8, opt_8);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_9.length() > 0) {
				ins(ps, pollID, 9, opt_9);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_10.length() > 0) {
				ins(ps, pollID, 10, opt_10);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_11.length() > 0) {
				ins(ps, pollID, 11, opt_11);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_12.length() > 0) {
				ins(ps, pollID, 12, opt_12);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_13.length() > 0) {
				ins(ps, pollID, 13, opt_13);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_14.length() > 0) {
				ins(ps, pollID, 14, opt_14);
				cont = true;
			} else {
				cont = false;
			}
			if (cont == true && opt_15.length() > 0) {
				ins(ps, pollID, 15, opt_15);
				cont = true;
			} else {
				cont = false;
			}
			// ps.close();
			// cn.commit();
		} catch (Exception e) {
			logger
					.warn(
							"exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - exception ignored", e); //$NON-NLS-1$

			// if (cn != null) {
			// cn.rollback();
			// }

		} finally {
			// cn.setAutoCommit(true);
			// Torque.closeConnection(cn);

		}

		JetUtil.redirect(vc, response, request.getContextPath()
				+ "/do/admin/surveys");

		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return null;
	}

	private void ins(PreparedStatement ps, int pollID_, int voteID, String txt)
			throws SQLException {
		if (logger.isDebugEnabled()) {
			logger.debug("ins(PreparedStatement, int, int, String) - start"); //$NON-NLS-1$
		}

		ps.setInt(1, pollID_);
		ps.setString(2, txt);
		ps.setInt(3, voteID);
		ps.execute();

		if (logger.isDebugEnabled()) {
			logger.debug("ins(PreparedStatement, int, int, String) - end"); //$NON-NLS-1$
		}
	}
}
